/**
 * @author Ahammer
 * @date   2009 11
 * @update
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import main.IqmTools;
import main.Tank;


public class IqmDFTGUI  extends IqmOperatorJFrame
							  implements IqmOperatorGUIBuilder, ActionListener, AdjustmentListener {

	private  ParameterBlockJAI    pb         = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame   opGUIFrame = null;
	//private  IqmCalcImageGUI 	  opGUIFrame = null;  //actionPerformed events werden in dieser Klasse abgehandelt
	
		
	private  JRadioButtonMenuItem buttScaleNone		= null;
	private  JRadioButtonMenuItem buttScaleUnit		= null;
	private  JRadioButtonMenuItem buttScaleDim	    = null;
	private  JRadioButtonMenuItem buttScaleHidden	= null;
	private  JToolBar 			  jToolBarScale	    = null;
	private  ButtonGroup 		  buttGroupScale	= null;
	
	private JPanel 				 jPanelResultOptions 	 = null;
	private JLabel 				 jLabelResultOptions	 = null;
	private JToolBar 			 jToolBarResultOptions	 = null;
	private ButtonGroup 		 buttGroupResultOptions	 = null;
	private JRadioButtonMenuItem buttResultOptionsHidden = null;
	private JRadioButtonMenuItem buttMagnitudeSquared	 = null;
	private JRadioButtonMenuItem buttMagnitude			 = null;
	private JRadioButtonMenuItem buttPhase			     = null;
	private JRadioButtonMenuItem buttReal			     = null;
	private JRadioButtonMenuItem buttImaginary			 = null;
	
	
	/**
	 * constructor
	 */
	public IqmDFTGUI() {

	}

	
	/**
	 * This method sets all  variables to null
	 */
	private void resetVariables(){
		pb         = null; 
		//opGUIFrame = null; 
		
		//jToolBarRGB  = null;

	}
	/**
	 * This method sets all parameters to null
	 */
	private void resetParameters(){
		//butt       = null;
		//butt       = null;
	}

	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pb = arg;
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){

		if (buttScaleNone.isSelected()) pb.setParameter("Scale", 0);
		if (buttScaleUnit.isSelected()) pb.setParameter("Scale", 1);
		if (buttScaleDim.isSelected())	pb.setParameter("Scale", 2);
		
		if (buttMagnitudeSquared.isSelected()) pb.setParameter("ResultOptions", 0);
		if (buttMagnitude.isSelected())        pb.setParameter("ResultOptions", 1);
		if (buttPhase.isSelected())            pb.setParameter("ResultOptions", 2);
		if (buttReal.isSelected())             pb.setParameter("ResultOptions", 3);
		if (buttImaginary.isSelected())        pb.setParameter("ResultOptions", 4);

		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmDFTGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
        //-------------------------------------------------

        if (pb.getIntParameter("Scale") == 0)     buttScaleNone.setSelected(true);
        if (pb.getIntParameter("Scale") == 1)     buttScaleUnit.setSelected(true);
        if (pb.getIntParameter("Scale") == 2)     buttScaleDim.setSelected(true);
               
		if (pb.getIntParameter("ResultOptions") == 0)     buttMagnitudeSquared.setSelected(true);
		if (pb.getIntParameter("ResultOptions") == 1)     buttMagnitude.setSelected(true);
		if (pb.getIntParameter("ResultOptions") == 2)     buttPhase.setSelected(true);
		if (pb.getIntParameter("ResultOptions") == 3)     buttReal.setSelected(true);
		if (pb.getIntParameter("ResultOptions") == 4)     buttImaginary.setSelected(true);
	}

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//System.out.println("IqmDFTGUI: createAndShowGUI invoked");
		//opGUIFrame = new IqmDFTGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("DFT");
		//PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+300);
		//this.setPreferredSize(new Dimension(800, 600));

		this.getJContentPane().add(getJToolBarScale(),        getGridBagConstraintsScale());
		this.getJContentPane().add(getJPanelResultOptions(), getGridBagConstraintsResultOptions());
	
		this.pack();
		//this.repaint();
		if (pb == null) pb = new ParameterBlockJAI("iqmdft");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqmthreshold"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pb);
		this.setParameterValues();
	}
	private  GridBagConstraints getGridBagConstraintsPreSets(){
		GridBagConstraints gridBagConstraintsPreSets = new GridBagConstraints();
		gridBagConstraintsPreSets.gridx = 1;
		gridBagConstraintsPreSets.gridy = 6;
		gridBagConstraintsPreSets.gridwidth = 3;//?
		gridBagConstraintsPreSets.insets = new Insets(15,0,0,0);
		//gridBagConstraintsPreSets.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsPreSets;
	}
	private  GridBagConstraints getGridBagConstraintsScale(){
		GridBagConstraints gridBagConstraintsScale = new GridBagConstraints();
		gridBagConstraintsScale.gridx = 1;
		gridBagConstraintsScale.gridy = 7;
		gridBagConstraintsScale.gridwidth = 3;
		gridBagConstraintsScale.insets = new Insets(10,0,0,0);

		//gridBagConstraintsScale.anchor = GridBagConstraints.LINE_START;
		//gridBagConstraintsScale.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsScale;
	}
	private  GridBagConstraints getGridBagConstraintsResultOptions(){
		GridBagConstraints gridBagConstraintsResultOptions = new GridBagConstraints();
		gridBagConstraintsResultOptions.gridx = 1;
		gridBagConstraintsResultOptions.gridy = 8;
		gridBagConstraintsResultOptions.gridwidth = 3;//?
		gridBagConstraintsResultOptions.insets = new Insets(10,0,20,0);
		//gridBagConstraintsResultOptions.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsResultOptions;
	}

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method closes and destroys the GUI
	 * 
	 * @return void
	 */
//	public void destroyGUI() {
//		if (this != null){
//					
//			//this.closeAndDestroyGUI();
//			//resetVariables();
//			//folgendes geht auch!?
//			this.setVisible(false);
//			this.dispose();
//			//this = null;
//		}
//	}
	/**
	 * This method updates the GUI
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){

	}

    //------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: ScaleNone
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonScaleNone() {
		if (buttScaleNone == null) {
			buttScaleNone  = new JRadioButtonMenuItem();
			buttScaleNone.setText("Scaling None");
			buttScaleNone.setToolTipText("no scaling");
			buttScaleNone.addActionListener(this);
			buttScaleNone.setActionCommand("parameter");
		}
		return buttScaleNone;
	}
	/**
	 * This method initializes the Option: ScaleUnit
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonScaleUnit() {
		if (buttScaleUnit == null) {
			buttScaleUnit  = new JRadioButtonMenuItem();
			buttScaleUnit.setText("Scale Unitary");
			buttScaleUnit.setToolTipText("scaling with sqrt(width + height)");
			buttScaleUnit.addActionListener(this);
			buttScaleUnit.setActionCommand("parameter");
		}
		return buttScaleUnit;
	}
	/**
	 * This method initializes the Option: ScaleDim
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonScaleDim() {
		if (buttScaleDim == null) {
			buttScaleDim  = new JRadioButtonMenuItem();
			buttScaleDim.setText("Scale Dimensions");
			buttScaleDim.setToolTipText("scaling with (width*height)");
			buttScaleDim.addActionListener(this);
			buttScaleDim.setActionCommand("parameter");
		}
		return buttScaleDim;
	}


	/**
	 * This method initializes the hidden Button option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonScaleHidden() {
		//if (buttScaleHidden == null) {
			buttScaleHidden  = new JRadioButtonMenuItem();
			buttScaleHidden.setText("Hidden");
			buttScaleHidden.setVisible(false);
		//}
		return buttScaleHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarScale() {
//		if (jToolBarScale == null) {
			jToolBarScale = new JToolBar("Scale");
			jToolBarScale.setOrientation(1); //Vertical orientation
			jToolBarScale.add(getJRadioButtonScaleNone());			
			jToolBarScale.add(getJRadioButtonScaleUnit());			
			jToolBarScale.add(getJRadioButtonScaleDim());			
			jToolBarScale.add(getJRadioButtonScaleHidden());

			//jToolBarScale.addSeparator();
			this.setButtonGroupScale();	//Gruppieren der JRadioButtons
//		}
		return jToolBarScale;
	}
	private  void setButtonGroupScale() {
		//if (ButtonGroup buttGroupScale == null) {
			buttGroupScale = new ButtonGroup();
			buttGroupScale.add(buttScaleNone);
			buttGroupScale.add(buttScaleUnit);
			buttGroupScale.add(buttScaleDim);
			buttGroupScale.add(buttScaleHidden);		
	}
	//------------------------------------------------------------------------------------

	/**
	 * This method initializes the Option: MagnitudeSquared
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMagnitudeSquared() {
		if (buttMagnitudeSquared == null) {
			buttMagnitudeSquared  = new JRadioButtonMenuItem();
			buttMagnitudeSquared.setText("Magnitude^2");
			buttMagnitudeSquared.setPreferredSize(new Dimension(105,10));
			buttMagnitudeSquared.setToolTipText("calculates the squared magnitude");
			buttMagnitudeSquared.addActionListener(this);
			buttMagnitudeSquared.setActionCommand("parameter");
		}
		return buttMagnitudeSquared;
	}
	/**
	 * This method initializes the Option: Magnitude
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMagnitude() {
		if (buttMagnitude == null) {
			buttMagnitude  = new JRadioButtonMenuItem();
			buttMagnitude.setText("Magnitude");
			buttMagnitude.setPreferredSize(new Dimension(95,10));
			buttMagnitude.setToolTipText("calculates the magnitude");
			buttMagnitude.addActionListener(this);
			buttMagnitude.setActionCommand("parameter");
		}
		return buttMagnitude;
	}
	/**
	 * This method initializes the Option: Phase
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonPhase() {
		if (buttPhase == null) {
			buttPhase  = new JRadioButtonMenuItem();
			buttPhase.setText("Phase");
			buttPhase.setPreferredSize(new Dimension(60,10));
			buttPhase.setToolTipText("calculates the phase");
			buttPhase.addActionListener(this);
			buttPhase.setActionCommand("parameter");
			//buttPhase.setEnabled(false);
		}
		return buttPhase;
	}
	/**
	 * This method initializes the Option: Real
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonReal() {
		if (buttReal == null) {
			buttReal  = new JRadioButtonMenuItem();
			buttReal.setText("Real");
			buttReal.setPreferredSize(new Dimension(60,10));
			buttReal.setToolTipText("calculates the real part");
			buttReal.addActionListener(this);
			buttReal.setActionCommand("parameter");
			//buttReal.setEnabled(false);
		}
		return buttReal;
	}
	/**
	 * This method initializes the Option: Imaginary
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonImaginary() {
		if (buttImaginary == null) {
			buttImaginary  = new JRadioButtonMenuItem();
			buttImaginary.setText("Imaginary");
			buttImaginary.setPreferredSize(new Dimension(60,10));
			buttImaginary.setToolTipText("calculates the imaginary part");
			buttImaginary.addActionListener(this);
			buttImaginary.setActionCommand("parameter");
			//buttImaginary.setEnabled(false);
		}
		return buttImaginary;
	}
	/**
	 * This method initializes the Option: Hidden
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonResultOptionsHidden() {
		if (buttResultOptionsHidden == null) {
			buttResultOptionsHidden  = new JRadioButtonMenuItem();
			buttResultOptionsHidden.setText("ResultOptionsHidden");
			buttResultOptionsHidden.setVisible(false);	
		}
		return buttResultOptionsHidden;
	}

	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarResultOptions() {
//		if (jToolBarResultOptions == null) {
			jToolBarResultOptions = new JToolBar("ResultOptions");
			jToolBarResultOptions.setOrientation(1); //0 Horizontal orientation
			jToolBarResultOptions.add(getJRadioButtonMagnitudeSquared());			
			jToolBarResultOptions.add(getJRadioButtonMagnitude());			
			jToolBarResultOptions.add(getJRadioButtonPhase());			
			jToolBarResultOptions.add(getJRadioButtonReal());			
			jToolBarResultOptions.add(getJRadioButtonImaginary());			
			jToolBarResultOptions.add(getJRadioButtonResultOptionsHidden());

			//jToolBarResultOptions.addSeparator();
			this.setButtonGroupResultOptions();	//Gruppieren der JRadioButtons
//		}
		return jToolBarResultOptions;
	}
	private  void setButtonGroupResultOptions() {
		//if (ButtonGroup buttGroupResultOptions == null) {
			buttGroupResultOptions = new ButtonGroup();
			buttGroupResultOptions.add(buttMagnitudeSquared);			
			buttGroupResultOptions.add(buttMagnitude);
			buttGroupResultOptions.add(buttPhase);
			buttGroupResultOptions.add(buttReal);
			buttGroupResultOptions.add(buttImaginary);
			buttGroupResultOptions.add(buttResultOptionsHidden);	
	}
	
	/**
	 * This method initializes jPanelResultOptions	
	 * @return javax.swing.JPanel	
	 */
	private  JPanel getJPanelResultOptions() {
		if (jPanelResultOptions == null) {
			jPanelResultOptions = new JPanel();	
			jPanelResultOptions.setLayout(new BorderLayout());
			jPanelResultOptions.setPreferredSize(new Dimension(150, 130));					
			jLabelResultOptions = new JLabel("Result: ");
			//jLabelResultOptions.setPreferredSize(new Dimension(60,10));
			jPanelResultOptions.add(jLabelResultOptions, BorderLayout.WEST);
			jPanelResultOptions.add(getJToolBarResultOptions(), BorderLayout.CENTER);		
		}
		return jPanelResultOptions	;
	}


	
	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmDFTGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmDFT".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			if (e.getSource() == buttMagnitude){
			}
			if (e.getSource() == buttMagnitudeSquared){		
			}
			if (e.getSource() == buttPhase){		
			}
			if (e.getSource() == buttReal){		
			}
			if (e.getSource() == buttImaginary){		
			}
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}
	
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}


	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		Object obE = e.getSource();
	
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmDFTGUI gui = new IqmDFTGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}



	
}//END
